package com.zhoug.common3.base.webview;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

/**
 * 网页Activity启动器
 * @Author 35574
 * @Date 2021/10/18
 * @Description
 */
public class WebPageLauncher {
    public static final String PARAM_TITLE="title";//标题
    public static final String PARAM_URL="url";//url
    public static final String PARAM_CONTENT_SOURCE="contentSource";//网页内容
    public static final String PARAM_DEFAULT_FONT_SIZE="defaultFontSize";//默认文字大小
    public static final String PARAM_JAVASCRIPT_ENABLED="javaScriptEnabled";//启用JavaScript

    private Bundle configArgs;

    public WebPageLauncher() {
        configArgs = new Bundle();
    }

    /**
     * 标题
     *
     * @param title
     * @return
     */
    public WebPageLauncher setTitle(String title) {
        configArgs.putString(PARAM_TITLE,title);
        return this;
    }

    /**
     * 网页Url
     *
     * @param url
     * @return
     */
    public WebPageLauncher setUrl(String url) {
        configArgs.putString(PARAM_URL,url);
        return this;
    }

    /**
     * 网页Url
     *
     * @param contentSource
     * @return
     */
    public WebPageLauncher setContentSource(String contentSource) {
        configArgs.putString(PARAM_CONTENT_SOURCE,contentSource);
        return this;
    }
    /**
     * 默认文字大小
     * @param defaultFontSize
     * @return
     */
    public WebPageLauncher setDefaultFontSize(int defaultFontSize) {
        configArgs.putInt(PARAM_DEFAULT_FONT_SIZE,defaultFontSize);
        return this;
    }

    /**
     * 是否启用JavaScript
     * @param javaScriptEnabled
     * @return
     */
    public WebPageLauncher setJavaScriptEnabled(boolean javaScriptEnabled) {
        configArgs.putBoolean(PARAM_JAVASCRIPT_ENABLED,javaScriptEnabled);
        return this;
    }


    private Intent getIntent(Context context,Class<? extends Activity> cls) {
        Intent intent = new Intent(context,cls);
        intent.putExtras(configArgs);
        return intent;
    }

    /**
     * start
     *
     * @param context
     */
    public void start(Context context,Class<? extends Activity> cls) {
        context.startActivity(getIntent(context,cls));
    }

    /**
     * @param activity
     * @param requestCode
     */
    public void startForResult(Activity activity,Class<? extends Activity> cls, int requestCode) {
        activity.startActivityForResult(getIntent(activity,cls), requestCode);
    }

    /**
     * @param fragment
     * @param requestCode
     */
    public void startForResult(Fragment fragment,Class<? extends Activity> cls, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            fragment.startActivityForResult(getIntent(fragment.getContext(),cls), requestCode);
        }else{
            fragment.startActivityForResult(getIntent(fragment.getActivity(),cls), requestCode);
        }
    }


}
